package com.hera.base.web.vaadin;

import static com.hera.base.util.InputData.getStringData;
import static com.hera.base.util.InputData.getStringDataCase;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.Constantes;
import com.hera.base.web.BaseUI;
import com.hera.base.web.SimpleAlertDialog;
//import com.hera.process.config.domain.LoginMethod;
//import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.hera.process.config.rest.delegate.SecurityDelegate;
import com.hera.process.security.domain.User;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class PortalLogin extends BaseUI implements ClickListener{

	@AutoGenerated
	private VerticalLayout mainLayout;


	@AutoGenerated
	private VerticalLayout lytConenedorLogin;


	@AutoGenerated
	private Panel pnlLogin;


	@AutoGenerated
	private VerticalLayout lytLogin;


	@AutoGenerated
	private HorizontalLayout horizontalLayout_3;


	@AutoGenerated
	private Button btnLogin;


	@AutoGenerated
	private HorizontalLayout horizontalLayout_2;


	@AutoGenerated
	private PasswordField txtPassword;


	@AutoGenerated
	private Label lblPassword;


	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;


	@AutoGenerated
	private TextField txtUsuario;


	@AutoGenerated
	private Label lblUsuario;


	@AutoGenerated
	private HorizontalLayout horizontalLayout_4;


	@AutoGenerated
	private Embedded imgCabezera;


	/**
	 * 
	 */
	private static final long serialVersionUID = 6840250442743186139L;


	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	private static Logger log = Logger.getLogger(PortalLogin.class);	
	

	@Autowired
	SecurityDelegate securityDelegate;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public PortalLogin() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
	}
	
	@Override
	protected void postConstruct() {
		cargarFormulario();
		
	}
	
	private void cargarFormulario() {
		
		mainLayout.setStyleName("desktopLogin");
		Resource resCabecera=new ThemeResource("images/logo-flexitraking.png");
		imgCabezera.setSource(resCabecera);
		//btnLogin.setStyleName("buttoncust");
		horizontalLayout_3.setStyleName("buttoncust");
		btnLogin.setDescription("Ingresar al Sistema");
		btnLogin.addClickListener(this);
		pnlLogin.addStyleName("panelLogin");
		
		txtUsuario.setCaption("Usuario:");
		//txtUsuario.setIcon(new ThemeResource("images/trash1.png"));
		txtPassword.setCaption("Password:");
		//txtPassword.setIcon(new ThemeResource("images/trash1.png"));
		txtUsuario.setValue("U00002");
		txtPassword.setValue("123");
	}

	
	
	public void limpiar(){
		txtUsuario.setValue("");
		txtPassword.setValue("");
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnLogin)){			
			if(validarUsuario()){
				PrincipalUI principalUI=new PrincipalUI();
				setCompositionRoot(principalUI);
			}
		}		
	}
	
	private boolean validarUsuario() {
		//try{
			String usuario, password;
			
			usuario=getStringData(txtUsuario,"usuario");
			password=getStringDataCase(txtPassword,"password");
			User user1= new User();
			user1.setUserLogin(usuario);
			user1.setPassword(password);
			User user2=securityDelegate.validateUser(user1);			
			if(user2==null){
				mostrarAlerta(new SimpleAlertDialog(), Constantes.CODIGO_MENSAJE.VALIDACION_USUARIO, "Usuario o Contraseña es Invalido");
				
				return false;
			}else{
				setDatoUsuarioLogin(user2);
				return true;
			}
		/*}catch(Exception e){
			LoginException be=new LoginException(Constantes.CODIGO_MENSAJE.VALIDACION_USUARIO);
			be.setException(e);
			throws be;*/
			//mostrarAlerta(new SimpleAlertDialog(), Constantes.CODIGO_MENSAJE.VALIDACION_USUARIO, e.getMessage());
			//return false;
		//}
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// lytConenedorLogin
		lytConenedorLogin = buildLytConenedorLogin();
		mainLayout.addComponent(lytConenedorLogin);
		mainLayout.setComponentAlignment(lytConenedorLogin, new Alignment(48));
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildLytConenedorLogin() {
		// common part: create layout
		lytConenedorLogin = new VerticalLayout();
		lytConenedorLogin.setImmediate(false);
		lytConenedorLogin.setWidth("-1px");
		lytConenedorLogin.setHeight("-1px");
		lytConenedorLogin.setMargin(false);
		
		// pnlLogin
		pnlLogin = buildPnlLogin();
		lytConenedorLogin.addComponent(pnlLogin);
		
		return lytConenedorLogin;
	}

	@AutoGenerated
	private Panel buildPnlLogin() {
		// common part: create layout
		pnlLogin = new Panel();
		pnlLogin.setImmediate(false);
		pnlLogin.setWidth("300px");
		pnlLogin.setHeight("350px");
		
		// lytLogin
		lytLogin = buildLytLogin();
		pnlLogin.setContent(lytLogin);
		
		return pnlLogin;
	}

	@AutoGenerated
	private VerticalLayout buildLytLogin() {
		// common part: create layout
		lytLogin = new VerticalLayout();
		lytLogin.setImmediate(false);
		lytLogin.setWidth("100.0%");
		lytLogin.setHeight("100.0%");
		lytLogin.setMargin(false);
		
		// horizontalLayout_4
		horizontalLayout_4 = buildHorizontalLayout_4();
		lytLogin.addComponent(horizontalLayout_4);
		lytLogin.setComponentAlignment(horizontalLayout_4, new Alignment(24));
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		lytLogin.addComponent(horizontalLayout_1);
		lytLogin.setComponentAlignment(horizontalLayout_1, new Alignment(24));
		
		// horizontalLayout_2
		horizontalLayout_2 = buildHorizontalLayout_2();
		lytLogin.addComponent(horizontalLayout_2);
		lytLogin.setComponentAlignment(horizontalLayout_2, new Alignment(48));
		
		// horizontalLayout_3
		horizontalLayout_3 = buildHorizontalLayout_3();
		lytLogin.addComponent(horizontalLayout_3);
		lytLogin.setComponentAlignment(horizontalLayout_3, new Alignment(20));
		
		return lytLogin;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_4() {
		// common part: create layout
		horizontalLayout_4 = new HorizontalLayout();
		horizontalLayout_4.setImmediate(false);
		horizontalLayout_4.setWidth("-1px");
		horizontalLayout_4.setHeight("-1px");
		horizontalLayout_4.setMargin(false);
		
		// imgCabezera
		imgCabezera = new Embedded();
		imgCabezera.setImmediate(false);
		imgCabezera.setWidth("80px");
		imgCabezera.setHeight("80px");
		imgCabezera.setSource(new ThemeResource(
				"img/component/embedded_icon.png"));
		imgCabezera.setType(1);
		imgCabezera.setMimeType("image/png");
		horizontalLayout_4.addComponent(imgCabezera);
		
		return horizontalLayout_4;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		horizontalLayout_1.setSpacing(true);
		
		// lblUsuario
		lblUsuario = new Label();
		lblUsuario.setStyleName("etiqueta");
		lblUsuario.setEnabled(false);
		lblUsuario.setImmediate(false);
		lblUsuario.setVisible(false);
		lblUsuario.setWidth("60px");
		lblUsuario.setHeight("-1px");
		lblUsuario.setValue("Usuario:");
		horizontalLayout_1.addComponent(lblUsuario);
		horizontalLayout_1.setComponentAlignment(lblUsuario, new Alignment(48));
		
		// txtUsuario
		txtUsuario = new TextField();
		txtUsuario.setStyleName("fieldRequired");
		txtUsuario.setImmediate(false);
		txtUsuario.setWidth("150px");
		txtUsuario.setHeight("-1px");
		horizontalLayout_1.addComponent(txtUsuario);
		horizontalLayout_1.setComponentAlignment(txtUsuario, new Alignment(6));
		
		return horizontalLayout_1;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_2() {
		// common part: create layout
		horizontalLayout_2 = new HorizontalLayout();
		horizontalLayout_2.setImmediate(false);
		horizontalLayout_2.setWidth("-1px");
		horizontalLayout_2.setHeight("-1px");
		horizontalLayout_2.setMargin(false);
		horizontalLayout_2.setSpacing(true);
		
		// lblPassword
		lblPassword = new Label();
		lblPassword.setStyleName("etiqueta");
		lblPassword.setEnabled(false);
		lblPassword.setImmediate(false);
		lblPassword.setVisible(false);
		lblPassword.setWidth("60px");
		lblPassword.setHeight("-1px");
		horizontalLayout_2.addComponent(lblPassword);
		
		// txtPassword
		txtPassword = new PasswordField();
		txtPassword.setStyleName("fieldRequired");
		txtPassword.setImmediate(false);
		txtPassword.setWidth("150px");
		txtPassword.setHeight("-1px");
		horizontalLayout_2.addComponent(txtPassword);
		
		return horizontalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_3() {
		// common part: create layout
		horizontalLayout_3 = new HorizontalLayout();
		horizontalLayout_3.setImmediate(false);
		horizontalLayout_3.setWidth("-1px");
		horizontalLayout_3.setHeight("-1px");
		horizontalLayout_3.setMargin(false);
		horizontalLayout_3.setSpacing(true);
		
		// btnLogin
		btnLogin = new Button();
		btnLogin.setCaption("Ingresar");
		btnLogin.setImmediate(true);
		btnLogin.setWidth("150px");
		btnLogin.setHeight("-1px");
		horizontalLayout_3.addComponent(btnLogin);
		horizontalLayout_3.setComponentAlignment(btnLogin, new Alignment(6));
		
		return horizontalLayout_3;
	}

	

}
